//
//  PictureCommentViewCell.m
//  Product_NEWS
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "PictureCommentViewCell.h"
#import "UIImageView+WebCache.h"

@interface PictureCommentViewCell ()
@property (nonatomic, retain) UIImageView *profileImageView;
@property (nonatomic, retain) UIImageView *textBackground;
@property (nonatomic, retain) WordTite *userNameLabel;
@property (nonatomic, retain) titleLabel *contentLable;
@end

@implementation PictureCommentViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
        
    }
  
    return self;
}


- (void)createSubViews
{
    
    
    
    // 文字背景图片
    self.textBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , 70)];
   
    [self.textBackground.layer setBorderWidth:1.0];
    // 设置边框颜色
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 230/255.0, 230/255.0, 230/255.0, 1 }); // 灰色
    [self.textBackground.layer setBorderColor:colorref];
    self.textBackground.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    [self addSubview:self.textBackground];
    
    // 用户头像
    self.profileImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
    // 设置图片圆角
    // 1.设置图片边框为隐藏状态
    self.profileImageView.layer.masksToBounds = YES;
    // 2.设置图片圆角
    self.profileImageView.layer.cornerRadius = 25; // 为imageView大小的一半即为圆
    [self addSubview:self.profileImageView];
    
    // 用户名
    self.userNameLabel = [[WordTite alloc] initWithFrame:CGRectMake(80 * SCREEN_WIDTH, self.profileImageView.frame.origin.y, 200*SCREEN_WIDTH, 20 *SCREEN_HEIGHT)];
  
    self.userNameLabel.font = [UIFont systemFontOfSize:13*SCREEN_WIDTH];
   
    [self addSubview:self.userNameLabel];
    
    // 评论内容
    self.contentLable = [[titleLabel alloc] initWithFrame:CGRectMake(20*SCREEN_WIDTH, self.userNameLabel.frame.origin.y + 50 * SCREEN_HEIGHT, self.frame.size.width - 80*SCREEN_WIDTH, 20 *SCREEN_HEIGHT)];
    self.contentLable.font = [UIFont systemFontOfSize:14 *SCREEN_WIDTH];
 
    self.contentLable.numberOfLines = 0;
    self.contentLable.lineBreakMode = NSLineBreakByCharWrapping;
    [self addSubview:self.contentLable];
    
}
- (UIButton *)btnShare {
    if (!_btnShare) {
        self.btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnShare.frame = CGRectMake(315* SCREEN_WIDTH, 3*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT);
        [_btnShare setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    }
    return [[_btnShare retain] autorelease];
}
- (UIButton *)btnSave {
    if (!_btnSave) {
        self.btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSave.frame = CGRectMake(252* SCREEN_WIDTH, 0, 23* SCREEN_WIDTH, 23*SCREEN_HEIGHT);
        [_btnSave setImage:[UIImage imageNamed:@"收藏@2x"] forState:UIControlStateNormal];
    }
    return [[_btnSave retain] autorelease];
}

- (UIButton *)btnComment {
    if (!_btnComment) {
        self.btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnComment.frame = CGRectMake((355 - 160)* SCREEN_WIDTH, 3*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT);
        [_btnComment setImage:[UIImage imageNamed:@"评论"] forState:UIControlStateNormal];
    }
    return [[_btnComment retain] autorelease];
}
- (UIView *)toolBar {
    if (!_toolBar) {
        self.toolBar = [[[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 44* SCREEN_WIDTH, 375* SCREEN_WIDTH, 44*SCREEN_HEIGHT)] autorelease];
        self.toolBar.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        [self.toolBar  addSubview:self.btnSave];
        UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake((375 - 23 - 100)* SCREEN_WIDTH, 23*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT)];
        lbl1.text = @"收藏";
        lbl1.textColor = [NightModeHandle shareNightModeHandle].titleColor;
        lbl1.font = [UIFont systemFontOfSize:10*SCREEN_WIDTH];
        [self.toolBar addSubview:lbl1];
        [lbl1 release];
        
        [self.toolBar  addSubview:self.btnShare];
        UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(315* SCREEN_WIDTH, 23*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT)];
        lbl2.text = @"分享";
        lbl2.font = [UIFont systemFontOfSize:10*SCREEN_WIDTH];
        lbl2.textColor = [NightModeHandle shareNightModeHandle].titleColor;
        [self.toolBar addSubview:lbl2];
        [lbl2 release];
        
        [self.toolBar addSubview:self.btnComment];
        UILabel *lbl3 = [[UILabel alloc] initWithFrame:CGRectMake((375 - 20 - 160)* SCREEN_WIDTH, 23*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT)];
        lbl3.text = @"评论";
        lbl3.font = [UIFont systemFontOfSize:10*SCREEN_WIDTH];
        lbl3.textColor = [NightModeHandle shareNightModeHandle].titleColor;
        [self.toolBar addSubview:lbl3];
        [lbl3 release];
    }
    return [[_toolBar retain] autorelease];
}


- (void)setPicComModel:(PicCommentModel *)picComModel
{
    if (_picComModel != picComModel) {
        _picComModel = picComModel;
    }
    
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:_picComModel.profile_image] placeholderImage:[UIImage imageNamed:@"background"]];
    
    self.userNameLabel.text = _picComModel.username;
    // 评论内容
    self.contentLable.text = _picComModel.content;
    self.contentLable.frame = CGRectMake(self.userNameLabel.frame.origin.x, self.userNameLabel.frame.origin.y + self.userNameLabel.frame.size.height, [[UIScreen mainScreen] bounds].size.width - 80 - 20, [[self class] heightForText:_picComModel.content width:[[UIScreen mainScreen] bounds].size.width - 80 - 20]);
    // 背景
    if (self.contentLable.frame.size.height > 40) {
        self.textBackground.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - 20, 30 + self.contentLable.frame.size.height + 5);
      
    }else{
        self.textBackground.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - 20, 50 + 20);
    }
    
    
}


// 自适应高度方法
+ (CGFloat)heightForText:(NSString *)text width:(CGFloat)width
{
    CGSize size = CGSizeMake(width, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0*SCREEN_WIDTH] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
